package com.upbos.upm.settings;

import com.upbos.core.ret.RetData;
import com.upbos.sso.UserContext;
import com.upbos.sso.entity.SsoUser;
import com.upbos.upm.entity.User;
import com.upbos.upm.entity.UserCfg;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.upbos.core.ret.RetData.success;

@RestController
@RequestMapping("upm/settings")
public class SettingsController {



    @Resource
    private SettingsService settingsService;


    @PostMapping("getUserCfg")
    public RetData getUserCfg(HttpServletRequest request, UserContext context) {
        SsoUser user = context.getSsoUser();
        return success(settingsService.getUser(user.getUid()));
    }

    @PostMapping("listLayoutCfg")
    public RetData listLayoutCfg(HttpServletRequest request, UserContext context) {
        SsoUser user = context.getSsoUser();
        return success(settingsService.listLayoutCfg(user.getUid()));
    }

    @PostMapping("getSecurityCfg")
    public RetData getSecurityCfg(HttpServletRequest request, UserContext userContext) throws Exception {
        SsoUser user = userContext.getSsoUser();
        return success(settingsService.getSecurityCfg(user.getUid()));
    }

    @PostMapping("saveUserCfg")
    public void saveUserCfg(User user) {
        settingsService.saveUserCfg(user);
    }

    @PostMapping("saveLayoutCfg")
    public void saveLayoutCfg(HttpServletRequest request, UserContext userContext, @RequestBody List<UserCfg> cfgList) {
        SsoUser user = userContext.getSsoUser();
        settingsService.saveLayoutCfg(user.getUid(), cfgList);
    }

    @PostMapping("restoreDefaultLayoutCfg")
    public void restoreDefaultLayoutCfg(HttpServletRequest request, UserContext userContext) {
        SsoUser user = userContext.getSsoUser();
        settingsService.restoreDefaultLayoutCfg(user.getUid());
    }
}
